package com.dyp.jpa.entity;

import lombok.Data;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
@Table(name="user")
public class User implements Serializable {
    @Id
    @Column(length = 100, unique = true, nullable = false)
    private String id;

    private String userName;
    private String userEmail;
    private String phoneNum;

    @OneToOne(targetEntity=Comment.class, mappedBy= "user",cascade = {CascadeType.ALL},
            fetch = FetchType.EAGER)
    private Comment comment;

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        hcb.append(id);
        hcb.append(userEmail);
        hcb.append(phoneNum);
        return hcb.toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        boolean  equals  =   false ;
        if  ( obj != null && User.class.isAssignableFrom(obj.getClass()) )  {
            final User other = (User) obj;
            equals  =  ( new EqualsBuilder( )
                    .append(id,other.id)
                    .append(userEmail, other.userEmail)
                    .append(phoneNum, other.phoneNum)).isEquals( );
        }
        return  equals;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.reflectionToString(this);
    }
}
